package com.example.huangyuanquan

import android.content.Context
import com.google.gson.Gson


object SharedPreferencesHelper {
    private const val STUDENT_KEY = "student_key"
    private const val PREFS_NAME = "student_prefs"

    fun saveStudent(context: Context, student: Student) {
        val sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.putString(STUDENT_KEY, student.toString())
        editor.apply()
    }

    fun loadStudent(context: Context): Student? {
        val sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
        return if (sharedPreferences.getString(STUDENT_KEY, null) != null) Gson().fromJson(
            sharedPreferences.getString(
                STUDENT_KEY, null
            ),
            Student::class.java
        ) else null
    }
}